package com.cooldatasoft.taskManager;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.vaadin.teemu.wizards.Wizard;

import com.cooldatasoft.domain.model.Project;
import com.cooldatasoft.domain.model.RolesEnum;
import com.cooldatasoft.domain.model.SelectedMainContent;
import com.cooldatasoft.domain.model.User;
import com.cooldatasoft.domain.model.UserProjectRoles;
import com.cooldatasoft.domain.service.ProjectsService;
import com.cooldatasoft.domain.service.UserProjectRolesService;
import com.cooldatasoft.security.TaskManagerSecurityManager;
import com.cooldatasoft.taskManager.component.CustomWizardStep;
import com.cooldatasoft.taskManager.user.gui.AddProjectView;
import com.cooldatasoft.taskManager.user.gui.AddTaskView;
import com.cooldatasoft.taskManager.user.gui.CalendarView;
import com.cooldatasoft.taskManager.user.gui.ChooseAddForm;
import com.cooldatasoft.taskManager.user.gui.LeftPanel;
import com.cooldatasoft.taskManager.user.gui.UserProjectsView;
import com.cooldatasoft.taskManager.user.gui.UsersView;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Configurable(preConstruction=true)
public class MainPage extends CustomComponent implements Property.ValueChangeListener {

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private VerticalLayout mainPanel_verticalLayout;
	@AutoGenerated
	private VerticalLayout headerLayout;
	@AutoGenerated
	private HorizontalLayout menuPanelLayout;
	@AutoGenerated
	private NativeButton menuHelpBtn;
	@AutoGenerated
	private NativeButton menuAddBtn;
	@AutoGenerated
	private NativeButton menuCalendarBtn;
	@AutoGenerated
	private NativeButton menuProjectsBtn;
	@AutoGenerated
	private NativeButton menuUsersBtn;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private NativeButton logout_button;
	@AutoGenerated
	private Label loggerUser;
	@AutoGenerated
	private Label loggedLab;
	@AutoGenerated
	private LeftPanel leftPanel;
	@AutoGenerated
	private ComboBox projects_comboBox;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	private static final long serialVersionUID = 1L;
	private Wizard wizard;

	@Autowired
	private TaskManagerSecurityManager securityManager;
	
	@Autowired
	private UserProjectRolesService userProjectRolesService;
	
	@Autowired
	private ProjectsService projectService;
	
	private SelectedMainContent mySelectedMainContent;
	
	protected Window addModalProjectWindow;
	protected Window addModalTaskWindow;

	public MainPage() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		logout_button.setCaption("");
		loggerUser.setValue(securityManager.getCurrentUser().getSurname() + " " + securityManager.getCurrentUser().getName());
		
		logout_button.addListener(new ClickListener() {
		
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				TaskManagerApplication.getInstance().close();
			}
		});
		customizeButtons();
		createWizard();
		inicializeProjectComboBox();
		loadProjectNativeSelect();
	}
	
	public void callModal(int mode) {
		switch(mode) {
		case 1:
			getWindow().addWindow(addModalProjectWindow);
			break;
		case 2:
			getWindow().addWindow(addModalTaskWindow);
			break;			
		}
	}

	private void createWizard() {
		VerticalLayout vl = new VerticalLayout();
		// instantiate the Wizard
		wizard = new Wizard();

		// add some steps that implement the WizardStep interface
		wizard.addStep(new CustomWizardStep("Add project / task", "images/1Step.png"));
		wizard.addStep(new CustomWizardStep("Manage projects / tasks", "images/2Step.png"));
		wizard.addStep(new CustomWizardStep("Users in project", "images/3Step.png"));
		wizard.addStep(new CustomWizardStep("Documentation", "images/DOc.png"));
		wizard.addStep(new CustomWizardStep("Calendar", "images/cal.png"));
		wizard.addStep(new CustomWizardStep("Source control", "images/sc.png"));
		
		wizard.getCancelButton().setVisible(false);
		wizard.getFinishButton().setVisible(false);
		
		// add the wizard to a layout
		vl.setSpacing(true);
		vl.setMargin(true);
		vl.setSizeFull();
		vl.addComponent(wizard);
		mainPanel_verticalLayout.addComponent(vl); 
		mySelectedMainContent = SelectedMainContent.OTHER;
	}
	
	private void inicializeProjectComboBox(){
		projects_comboBox.setInputPrompt("Please select project");
		projects_comboBox.addListener(this);
		projects_comboBox.setImmediate(true);
		projects_comboBox.setNullSelectionAllowed(false);
	}
	
	private void loadProjectNativeSelect(){
		projects_comboBox.removeAllItems();
		
		List<Project> projectsOfUser = new ArrayList<Project>();
		User currentUser = securityManager.getCurrentUser();
		
		// lector has privillege to see all projects
        if (currentUser.getSystemRole() == RolesEnum.LECTOR) {
        	List<Project> allProjects = projectService.getAllProjects();
        	for (Project project : allProjects) {
        		projectsOfUser.add(project);
        		projects_comboBox.addItem(project);
        		leftPanel.addItemToTree(project);
        	}
        } else {
			List<UserProjectRoles> projectsToUser = userProjectRolesService.getUserProjectRolesByIdUser(securityManager.getCurrentUser());
			if(projectsToUser != null){
				for (UserProjectRoles userProjectRoles : projectsToUser) {
					projectsOfUser.add(userProjectRoles.getIdProject());
				}
				for (Project project : projectsOfUser) {
					projects_comboBox.addItem(project);
					leftPanel.addItemToTree(project);
				}
			}
        }
		
		if (projectsOfUser.size() == 0 || projects_comboBox.getValue() == null) {
		     menuUsersBtn.setEnabled(false);
		     menuProjectsBtn.setEnabled(false);
		} else {
			 menuUsersBtn.setEnabled(true);
			 menuProjectsBtn.setEnabled(true);
		}
	}
	
	private void customizeButtons() {
		menuProjectsBtn.setDescription("Projects / Tasks");
		menuProjectsBtn.setCaption("");
		menuProjectsBtn.addListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				setProjectsView();
			}
		});
		menuUsersBtn.setDescription("Users in project");
		menuUsersBtn.setCaption("");
		menuUsersBtn.addListener(new ClickListener() {
	
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				setUsersView();
			}
		});
		menuCalendarBtn.setDescription("Calenedar");
		menuCalendarBtn.setCaption("");
		menuCalendarBtn.addListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				mainPanel_verticalLayout.removeAllComponents();
				CalendarView cal = new CalendarView();
				mainPanel_verticalLayout.addComponent(cal);
				mySelectedMainContent = SelectedMainContent.OTHER;
			}
		});
		menuAddBtn.setDescription("Add Project / Task");
		menuAddBtn.setCaption("");
		menuAddBtn.addListener(new ClickListener() {
		
			private static final long serialVersionUID = 1L;

			@SuppressWarnings("serial")
			@Override
			public void buttonClick(ClickEvent event) {
				mainPanel_verticalLayout.removeAllComponents();
				
				Window addModalWindow = new Window("Choose what do you want to create");
				ChooseAddForm chooseAddForm = new ChooseAddForm();
				chooseAddForm.setMp(getMainPage());
				addModalWindow.setContent(chooseAddForm);
				addModalWindow.setModal(true);
				addModalWindow.setWidth("300px");
				addModalWindow.setHeight("200px");
				addModalWindow.addListener(new Window.CloseListener() {
					
					public void windowClose(CloseEvent e) {
						setProjectsView();				
					}
				});
				
				addModalProjectWindow = new Window("Create new project");
				AddProjectView addProject = new AddProjectView();
				addProject.setMainPage(getMainPage());
				addModalProjectWindow.setContent(addProject);
				addModalProjectWindow.setModal(true);
				addModalProjectWindow.setWidth("770px");
				addModalProjectWindow.setHeight("520px");
				addModalProjectWindow.addListener(new Window.CloseListener() {
					
					public void windowClose(CloseEvent e) {
						setProjectsView();				
					}
				});
				
				addModalTaskWindow = new Window("Create new task");
				addModalTaskWindow.setContent(new AddTaskView());
				addModalTaskWindow.setModal(true);
				addModalTaskWindow.setWidth("440px");
				addModalTaskWindow.setHeight("400px");
				addModalTaskWindow.addListener(new Window.CloseListener() {
					
					public void windowClose(CloseEvent e) {
						setProjectsView();				
					}
				});

				getWindow().addWindow(addModalWindow);
				
				mySelectedMainContent = SelectedMainContent.OTHER;
			}
		});
		menuHelpBtn.setDescription("Tutorial");
		menuHelpBtn.setCaption("");
		menuHelpBtn.addListener(new ClickListener() {
	
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				mainPanel_verticalLayout.removeAllComponents();
				createWizard();
			}
		});
	}
	private MainPage getMainPage(){
		return this;
	}
	
	public void addProject(Project project) {
		leftPanel.addItemToTree(project);
		loadProjectNativeSelect();
	}
	
	public void removeProject(Project project) {
		loadProjectNativeSelect();
	}

	public void setLeftPanel(LeftPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// leftPanel
		leftPanel = new LeftPanel(this);
		leftPanel.setStyleName("leftPanel");
		leftPanel.setImmediate(false);
		leftPanel.setWidth("220px");
		leftPanel.setHeight("100.0%");
		mainLayout.addComponent(leftPanel, "top:120.0px;left:0.0px;");
		
		// headerLayout
		headerLayout = buildHeaderLayout();
		mainLayout.addComponent(headerLayout,
				"top:0.0px;right:0.0px;left:0.0px;");
		
		// mainPanel_verticalLayout
		mainPanel_verticalLayout = new VerticalLayout();
		mainPanel_verticalLayout.setStyleName("mainPanel");
		mainPanel_verticalLayout.setImmediate(false);
		mainPanel_verticalLayout.setWidth("100.0%");
		mainPanel_verticalLayout.setHeight("100.0%");
		mainPanel_verticalLayout.setMargin(false);
		mainLayout.addComponent(mainPanel_verticalLayout,
				"top:120.0px;left:220.0px;");
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildHeaderLayout() {
		// common part: create layout
		headerLayout = new VerticalLayout();
		headerLayout.setStyleName("headerLayout");
		headerLayout.setImmediate(false);
		headerLayout.setWidth("100.0%");
		headerLayout.setHeight("120px");
		headerLayout.setMargin(false);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		headerLayout.addComponent(horizontalLayout_1);
		headerLayout
				.setComponentAlignment(horizontalLayout_1, new Alignment(6));
		
		// menuPanelLayout
		menuPanelLayout = buildMenuPanelLayout();
		headerLayout.addComponent(menuPanelLayout);
		headerLayout.setComponentAlignment(menuPanelLayout, new Alignment(6));
		
		return headerLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setStyleName("userPanel");
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("220px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		// loggedLab
		loggedLab = new Label();
		loggedLab.setStyleName("loggedLabel");
		loggedLab.setImmediate(false);
		loggedLab.setWidth("-1px");
		loggedLab.setHeight("-1px");
		loggedLab.setValue("Logged as:");
		horizontalLayout_1.addComponent(loggedLab);
		horizontalLayout_1.setComponentAlignment(loggedLab, new Alignment(6));
		
		// loggerUser
		loggerUser = new Label();
		loggerUser.setImmediate(false);
		loggerUser.setWidth("-1px");
		loggerUser.setHeight("-1px");
		loggerUser.setValue("Label");
		horizontalLayout_1.addComponent(loggerUser);
		horizontalLayout_1.setComponentAlignment(loggerUser, new Alignment(20));
		
		// logout_button
		logout_button = new NativeButton();
		logout_button.setStyleName("logoutBtn");
		logout_button.setCaption("logout");
		logout_button.setImmediate(true);
		logout_button.setWidth("-1px");
		logout_button.setHeight("-1px");
		horizontalLayout_1.addComponent(logout_button);
		
		return horizontalLayout_1;
	}

	private void setProjectsView() {
		Project currentProject = (Project) projects_comboBox.getValue();
		if (currentProject != null) {
			mainPanel_verticalLayout.removeAllComponents();	
			UserProjectsView upr = new UserProjectsView(currentProject);
			upr.setLeftPanel(leftPanel);
			mainPanel_verticalLayout.addComponent(upr);
			mySelectedMainContent = SelectedMainContent.PROJECTS;
		}
	}
	
	private void setUsersView() {
		Project currentProject = (Project) projects_comboBox.getValue();
		if (currentProject != null) {
			mainPanel_verticalLayout.removeAllComponents();
			UsersView uservView = new UsersView(currentProject);
			mainPanel_verticalLayout.addComponent(uservView);
			mySelectedMainContent = SelectedMainContent.USERS;
		}
	}
	
	@AutoGenerated
	private HorizontalLayout buildMenuPanelLayout() {
		// common part: create layout
		menuPanelLayout = new HorizontalLayout();
		menuPanelLayout.setStyleName("menuPanel");
		menuPanelLayout.setImmediate(false);
		menuPanelLayout.setWidth("-1px");
		menuPanelLayout.setHeight("60px");
		menuPanelLayout.setMargin(false);
		
		// projects_comboBox
		projects_comboBox = new ComboBox();
		projects_comboBox.setDescription("Choose your project!");
		projects_comboBox.setImmediate(true);
		projects_comboBox.setWidth("200px");
		projects_comboBox.setHeight("25px");
		projects_comboBox.setTextInputAllowed(false);
		menuPanelLayout.addComponent(projects_comboBox);
		menuPanelLayout.setComponentAlignment(projects_comboBox, new Alignment(48));
		
		// menuUsersBtn
		menuUsersBtn = new NativeButton();
		menuUsersBtn.setStyleName("menu-users");
		menuUsersBtn.setCaption("NativeButton");
		menuUsersBtn.setImmediate(true);
		menuUsersBtn.setWidth("55px");
		menuUsersBtn.setHeight("55px");
		menuPanelLayout.addComponent(menuUsersBtn);
		menuPanelLayout.setComponentAlignment(menuUsersBtn, new Alignment(48));
		
		// menuProjectsBtn
		menuProjectsBtn = new NativeButton();
		menuProjectsBtn.setStyleName("menu-projects");
		menuProjectsBtn.setCaption("NativeButton");
		menuProjectsBtn.setImmediate(true);
		menuProjectsBtn.setWidth("55px");
		menuProjectsBtn.setHeight("55px");
		menuPanelLayout.addComponent(menuProjectsBtn);
		menuPanelLayout.setComponentAlignment(menuProjectsBtn,
				new Alignment(48));
		
		// menuCalendarBtn
		menuCalendarBtn = new NativeButton();
		menuCalendarBtn.setStyleName("menu-calendar");
		menuCalendarBtn.setCaption("NativeButton");
		menuCalendarBtn.setImmediate(true);
		menuCalendarBtn.setWidth("55px");
		menuCalendarBtn.setHeight("55px");
		menuPanelLayout.addComponent(menuCalendarBtn);
		menuPanelLayout.setComponentAlignment(menuCalendarBtn,
				new Alignment(48));
		
		// menuAddBtn
		menuAddBtn = new NativeButton();
		menuAddBtn.setStyleName("menu-add");
		menuAddBtn.setCaption("NativeButton");
		menuAddBtn.setImmediate(true);
		menuAddBtn.setWidth("55px");
		menuAddBtn.setHeight("55px");
		menuPanelLayout.addComponent(menuAddBtn);
		menuPanelLayout.setComponentAlignment(menuAddBtn, new Alignment(48));
		
		// menuCardsBtn
		menuHelpBtn = new NativeButton();
		menuHelpBtn.setStyleName("menu-help");
		menuHelpBtn.setCaption("NativeButton");
		menuHelpBtn.setImmediate(true);
		menuHelpBtn.setWidth("55px");
		menuHelpBtn.setHeight("55px");
		menuPanelLayout.addComponent(menuHelpBtn);
		menuPanelLayout.setComponentAlignment(menuHelpBtn, new Alignment(48));
		
		return menuPanelLayout;
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		// TODO Auto-generated method stub
		if(event.getProperty() == projects_comboBox){			
		
			// if haven't to be here, but for sure
			if (projects_comboBox.getValue() != null) {
				 menuUsersBtn.setEnabled(true);
				 menuProjectsBtn.setEnabled(true);
			}
			
			if (mySelectedMainContent == SelectedMainContent.PROJECTS) {
				setProjectsView();
			} 
			else if (mySelectedMainContent == SelectedMainContent.USERS) {
				setUsersView();
			}
		}
	}
	
   
}
